/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package imat;

import java.awt.CardLayout;
import java.awt.Dimension;
import java.text.DecimalFormat;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import se.chalmers.ait.dat215.project.ShoppingCart;
import se.chalmers.ait.dat215.project.ShoppingCartListener;
import se.chalmers.ait.dat215.project.ShoppingItem;

/**
 *
 * @author Andreas
 */
public class SideTrolleyPresenter implements ShoppingCartListener{
    private ShoppingCart shoppingCart;
    private SideTrolleyView sideTrolleyView;
    private DecimalFormat nf = new DecimalFormat();

    public SideTrolleyPresenter(SideTrolleyView view) {
        shoppingCart = DataHandler.getInstance().getShoppingCart();
        shoppingCart.addShoppingCartListener(this);
        sideTrolleyView = view;

        nf.setMinimumFractionDigits(2);
    }

    public void shoppingCartChanged() {
        sideTrolleyView.getShoppingItemsPanel().removeAll();
        sideTrolleyView.repaint();
        for (ShoppingItem item : shoppingCart.getItems()) {
            System.out.println(item.getProduct().getName() + " " + item.getAmount());
            addProduct(item);
        }
        updateTotalPrice();
    }

    public void updateTotalPrice() {
        sideTrolleyView.getTotalPriceLabel().setText(nf.format(shoppingCart.getTotal())+" kr");
    }

    public void addProduct(ShoppingItem item) {
        ShoppingItemView view = new ShoppingItemView(this);
        view.setShoppingItem(item);
        sideTrolleyView.getShoppingItemsPanel().add(view, 0);
        JPanel shoppingPanel = sideTrolleyView.getShoppingItemsPanel();
        int nr = shoppingPanel.getComponentCount();

        shoppingPanel.setPreferredSize(new Dimension(150, 10+nr*34));
        shoppingPanel.revalidate();
        sideTrolleyView.repaint();
    }

    public void save() {
        if (DataHandler.getInstance().getShoppingCart().getTotal() > 0) {
            String name = JOptionPane.showInputDialog(null, "Vad vill du döpa kundvagnen till?", "Spara kundvagn", JOptionPane.DEFAULT_OPTION);
            if (name != null)
                DataHandler.getInstance().saveCart(DataHandler.getInstance().getShoppingCart(), name);
        }
    }

    public void checkout() {
        if (DataHandler.getInstance().getShoppingCart().getTotal() > 0) {
            IMatView mainWindow = DataHandler.getInstance().getMainWindow();
            CardLayout cl = (CardLayout)(mainWindow.getViewPanel().getLayout());
            cl.show(mainWindow.getViewPanel(), "checkoutCard");
            mainWindow.getCheckoutView().updateReceipt();
            mainWindow.subMenuTitle.setText("");
            mainWindow.subMenuPanel.removeAll();
            mainWindow.subMenuPanel.validate();
            mainWindow.subMenuPanel.repaint();
            mainWindow.buttonPanel.setVisible(true);
        }
    }
}
